<?php

namespace App\V3\Api\Controllers;

use App\V3\Api\Models\SupplierCustomerModel;
use App\V3\Api\Services\SupplierCustomerService;
use \App\Http\Controllers\Api\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class SupplierCustomerController extends BaseController
{
    /**
     * 新建供应商或客户
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/13 11:25
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'name' => 'required',//客户\供应商名称、
//            'type' => [
//                'required',
//                Rule::In([0,1]),
//            ]
        ],[
            'name.required' => '请填写名称',
//            'type.required' => '类型缺失',
//            'type.in' => '类型不合法'
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $type = !isset($data['type'])?0:$data['type'];
        $data['compony_name'] = empty($data['compony_name'])?'':$data['compony_name'];
        $data['address'] = empty($data['address'])?'':$data['address'];

        $arr = [
            'compony_name' => $data['compony_name'],
            'name' => $data['name'],
            'phone' => isset($data['phone'])?$data['phone']:'',
            'address' => $data['address'],
            'home_phone' => isset($data['home_phone'])?$data['home_phone']:'',
            'remark' => isset($data['remark'])?$data['remark']:'',
            'main_id' => $this->getMainUserId(),
            'type' => $type,
            'user_id' => $this->getUserId(),
            'created_at' => time()
        ];

        $this->log('添加供应商和客户', $data);
        $id = SupplierCustomerModel::insertGetId($arr);
        return $this->success($id);
    }

    /**
     * 编辑供应商或客户信息
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/14 14:24
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' => 'required|integer',//供应商客户表主键id
            'name' => 'required',//客户\供应商名称、
//            'type' => [
//                'required',
//                Rule::In([0,1]),
//            ]
        ], [
            'id.required' => '参数`标识id`缺失',
            'id.integer' => '参数`标识id`类型错误',
            'name.required' => '请填写名称',
//            'type.required' => '类型缺失',
//            'type.in' => '类型不合法'
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $data['user_id'] = $this->getUserId();

        SupplierCustomerService::edit($data);

        $this->log('编辑供应商或客户', $data);
        return $this->success();
    }

    /**
     * 删除供应商或客户
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/14 14:51
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'ids' => 'required',//供应商客户表主键id
        ], [
            'ids.required' => '参数`标识id`缺失',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $idArr=explode(',', $data['ids']);
        $list=SupplierCustomerModel::whereIn('id',$idArr)->get();

        if(!empty($list)){

            $bool = SupplierCustomerModel::whereIn('id',$idArr)->update(['delete_flag'=>1]);
        }

        $this->log('删除供应商或客户', $list);
        return $this->success();
    }

    /**
     * 列表
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/14 15:44
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'pageSize' => 'required|integer',//页面大小
            'currentPage' => 'required|integer',//当前页
//            'type' => [
//                'required',
//                'integer',
//                Rule::In([1, 2, 0]),
//            ],//类型：0-供应商 1-客户 2-全部
        ], [
            'pageSize.required' => '参数`页面大小`缺失',
            'pageSize.integer' => '参数`页面大小`仅能为数字',
            'currentPage.required' => '参数`当前页码`缺失',
            'currentPage.integer' => '参数`当前页码`仅能为数字',
//            'type.required' => '参数`类型`缺失',
//            'type.integer' => '参数`类型`仅能为数字',
//            'type.in' => '参数`类型`不合法',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        //用户类型;0:主账号;1:子账号;
        $data['user_type'] = $this->getUserTpye();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        $data['is_share_customer'] = $this->isShareCustomer();
        $data['is_share_supplier'] = $this->isShareSupplier();
        $data['is_set_as_supplier'] = $this->isShareSupplier();
        $data['type'] = isset($data['type'])?1:0;
        $list = SupplierCustomerService::index($data);
        foreach ($list['data'] as &$v){

            if(!empty($v['phone'])){

                $v['phone']=explode(',', $v['phone']);
            }
        }
        return $this->success($list);
    }

    /**
     * 供应商或客户详细
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/14 15:53
     * @return \Illuminate\Http\JsonResponse
     */
    public function find(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' => 'required|integer',//供应商客户表主键id
        ], [
            'id.required' => '参数`标识id`缺失',
            'id.integer' => '参数`标识id`类型错误'
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $info = SupplierCustomerModel::select(
            'id', 'compony_name', 'name', 'phone', 'address','remark', 'created_at'
            )
            ->find($data['id']);

            if(!empty($info['phone'])){

                $info['phone']=explode(',', $info['phone']);
            }

        return $this->success($info);
    }

    /**
     * 下拉
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/14 16:07
     * @return \Illuminate\Http\JsonResponse
     */
    public function select(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'type' => [
                'required',
                Rule::in([0,1])
            ],//单据类型1:采购单;0:销售单;
        ], [
            'type.required' => '参数`单据类型`缺失',
            'type.in' => '参数`单据类型`不合法'
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        //单据类型：0-供应商 1-客户
       /*  if($this->getUserTpye()) { */
        $keyword = isset($data['keyword'])?$data['keyword']:'';
            if(!$data['type']) {

                $result = SupplierCustomerService::getSupplierList(
                    $data['type'],
                    $this->isShareCustomer(),
                    $this->isShareSupplier(),
                    $this->isSetAsSupplier(),
                    $this->getUserId(),
                    $this->getMainUserId(),
                    $keyword
                    );
            }
            else {
                $result = SupplierCustomerService::getCustomerList(
                    $data['type'],
                    $this->isShareCustomer(),
                    $this->getUserId(),
                    $this->getMainUserId(),
                    $keyword
                    );
            }
       /*  }
        else {

            $result = SupplierCustomerService::mainGetList($data['type'], $this->getMainUserId());
        } */

        return $this->success($result);
    }
}
